package com.niit.onlinemusic.controller;

import com.niit.onlinemusic.common.R;
import com.niit.onlinemusic.model.domain.SongList;
import com.niit.onlinemusic.model.request.SongListRequest;
import com.niit.onlinemusic.service.SongListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 歌单控制器
 *
 * @author zuwei522
 */
@Slf4j
@RestController
@RequestMapping("songList")
public class SongListController {

    private final SongListService songListService;

    public SongListController(SongListService songListService) {
        this.songListService = songListService;
    }

    /**
     * 根据风格查询歌单
     *
     * @param style 风格
     * @return
     * @author zuwei522
     * @since 2025/9/19 08:42
     */
    @GetMapping
    public R songList(@RequestParam(defaultValue = "") String style) {
        return songListService.getSongList(style);
    }

    /**
     * 添加歌单
     *
     * @param songListRequest 添加歌单请求体
     * @return R
     * @author zuwei522
     * @since 2025/9/27 23:05
     */
    @PostMapping("add")
    public R addSongList(@RequestBody SongListRequest songListRequest) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(songListRequest, songList);
        songList.setPic("/img/songListPic/default.jpg");
        log.info("添加歌单: {}", songList);
        if (songListService.save(songList)) {
            return R.success("添加成功");
        } else {
            return R.error("添加失败");
        }
    }

    @PostMapping("update")
    public R updateSongList(@RequestBody SongListRequest songListRequest) {
        SongList songList = new SongList();
        BeanUtils.copyProperties(songListRequest, songList);
        log.info("更新歌单: {}", songList);
        if (songListService.updateById(songList)) {
            return R.success("修改成功");
        } else {
            return R.error("更新失败");
        }
    }

    @GetMapping("delete")
    public R deleteSongList(@RequestParam("id") Integer id) {
        log.info("删除歌单: {}", id);
        if (songListService.removeById(id)) {
            return R.success("删除成功");
        } else {
            return R.error("删除失败");
        }
    }

    /**
     * 根据歌单标题模糊查询歌单
     *
     * @param title 歌单标题
     * @return R
     * @author zuwei522
     * @since 2025/9/29 14:54
     */
    @GetMapping("likeTitle/detail")
    public R getSongListByTitle(@RequestParam("title") String title) {
        log.info("根据歌单标题模糊查询歌单，歌单标题：{}", title);
        return songListService.searchSongListByTitle(title);
    }

}
